import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:goumao_app/Controller/gm_cw_danan.dart';
import 'package:goumao_app/Customs/gm_app_util.dart';
import 'package:goumao_app/Model/gm_my_pets.dart';

class GMMyPetScreen extends StatefulWidget {
  const GMMyPetScreen({super.key});

  @override
  // ignore: library_private_types_in_public_api
  _GMMyPetScreenState createState() => _GMMyPetScreenState();
}

class _GMMyPetScreenState extends State<GMMyPetScreen> {
  Future<GMMyPetsModel> _getMyPetsList() async {
    String jsonString =
        await rootBundle.loadString('assets/mydata/my_pets.json');
    Map<String, dynamic> jsonData = json.decode(jsonString);
    var articleModel = GMMyPetsModel.fromJson(jsonData);
    return articleModel;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: GMAppUtil.navText("我的宠物"),
      ),
      body: SafeArea(
        child: FutureBuilder<GMMyPetsModel>(
          future: _getMyPetsList(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const CircularProgressIndicator(); // 显示加载指示器
            } else if (snapshot.hasError) {
              return Text('Error: ${snapshot.error}');
            } else {
              GMMyPetsModel mModel = snapshot.data!;
              return ListView.builder(
                  itemCount: mModel.data!.petList!.length, // 列表总数
                  itemBuilder: (context, index) => GMMyPetItemView(
                        mData: mModel.data!.petList![index],
                      ));
            }
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => const GMCWDananScreen(),
            ),
          );
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

class GMMyPetItemView extends StatelessWidget {
  final GMPetList mData;
  const GMMyPetItemView({super.key, required this.mData});

  void showAlertDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return CupertinoAlertDialog(
          // title: Text('提示'),
          content: const Text('确定删除当前宠物吗?', style: TextStyle(color: Color(0xff1a1a1a), fontSize: 15, fontWeight: FontWeight.w500),),
          actions: <Widget>[
            CupertinoDialogAction(
              child: const Text('再想想', style: TextStyle(color: Color(0xff1a1a1a), fontSize: 15, fontWeight: FontWeight.w500),),
              onPressed: () {
                Navigator.of(context).pop(); // 关闭对话框
              },
            ),
            CupertinoDialogAction(
              child: const Text('确定', style: TextStyle(color: Color(0xff666666), fontSize: 15),),
              onPressed: () {
                // 执行确定操作
                Navigator.of(context).pop(); // 关闭对话框
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 100,
      padding: const EdgeInsets.fromLTRB(15, 0, 15, 0),
      // child: Center(child: Text('哈哈'),),
      child: Column(
        children: [
          // 你的 Container 内容
          SizedBox(
              height: 99,
              width: double.infinity,
              child: Row(
                // mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(10),
                    child: Image.network(
                      mData.coverUrl!,
                      width: 58,
                      height: 58,
                      fit: BoxFit.cover,
                    ),
                  ),
                  const SizedBox(
                    width: 10,
                  ),
                  SizedBox(
                    height: 48,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          mData.nickname!,
                          style: const TextStyle(
                              color: Color(
                                0xff1a1a1a,
                              ),
                              fontSize: 15,
                              fontWeight: FontWeight.w700),
                        ),
                        Text(
                          '${mData.categoryName!}  相伴${mData.accompanyDays!}',
                          style: const TextStyle(
                              color: Color(
                                0xff333333,
                              ),
                              fontSize: 13,
                              fontWeight: FontWeight.w200),
                        ),
                      ],
                    ),
                  ),
                  const Spacer(),
                  InkWell(
                    onTap: () => {
                      GMAppUtil.popFromBottomView(context, "宠物管理", [
                        ListItem(
                            text: '修改档案',
                            onTap: () => {
                                  Navigator.of(context).pop(),
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                      builder: (context) =>
                                          GMCWDananScreen(mData: mData,),
                                    ),
                                  )
                                }),
                        ListItem(
                            text: '删除',
                            onTap: () => {Navigator.of(context).pop(),showAlertDialog(context)})
                      ])
                    },
                    child: const Row(
                      children: [
                        Text("管理"),
                        Icon(
                          Icons.chevron_right,
                          color: Colors.grey,
                        )
                      ],
                    ),
                  )
                ],
              )),
          // 添加分割线
          const Divider(
            height: 1, // 分割线的高度
            color: Colors.grey,
          ),
        ],
      ),
    );
  }
}
